package stack;

import java.util.Arrays;

public class MyStack {
    public int[] elem;
    public int usedSize;

    public MyStack() {
        this.elem = new int[4];
    }

    // 放入元素
    public void push(int val) {
        if(isFull()) {
            // 如果放满了，二倍扩容
            this.elem = Arrays.copyOf(elem,2 * elem.length);
        }
        this.elem[this.usedSize++] = val;
    }
    // 获取栈顶元素并弹出
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空！");
        }
        usedSize--;
        return elem[usedSize];
    }
    // 获取栈顶元素
    public int peek() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空！");
        }
        return elem[usedSize-1];
    }
    // 是否为空
    public boolean isEmpty() {
        return usedSize == 0;
    }
    // 是否满了
    public boolean isFull() {
        return elem.length == usedSize;
    }
}
